<template>
  <xtx-bread>
    <xtx-bread-item to="/">首页</xtx-bread-item>
    <xtx-bread-item v-if="category.top" :to="`/category/${category.top.id}`">{{category.top.name}}</xtx-bread-item>
    <transition name="fade-right" mode="out-in">
      <xtx-bread-item v-if="category.sub" :key="category.sub.id">{{category.sub.name}}</xtx-bread-item>
    </transition>
  </xtx-bread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    // 获取二级分类的 地址在路由中
    const route = useRoute()
    // console.log(route.params.id)

    // 获取 vuex 中的类目数据
    const store = useStore()

    // 通过计算属性得到 二级类目的名称和id 一级类目的名称和id
    const category = computed(() => {
      const obj = {}
      store.state.category.list.forEach(top => {
        top.children && top.children.forEach(sub => {
          if (sub.id === route.params.id) {
            // 设置二级类目
            obj.sub = { id: sub.id, name: sub.name }
            // 设置一级类目
            obj.top = { id: top.id, name: top.name }
          }
        })
      })
      return obj
    })
    return { category }
  }
}
</script>

<style scoped lang='less'>

</style>
